﻿using System;
using DomainExample.DomainModel.ContactModel;
using DomainExample.Repository.Exceptions;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace DomainExample.Repository.Tests.ContactRepositoryTests
{
    [TestClass]
    public sealed class ContactRepositoryPersistTests : ContactRepositoryTests
    {
        [TestInitialize]
        public void SetUp()
        {
            CreateSchema();
        }

        [TestCleanup]
        public void TearDown()
        {
            DestroySchema();
        }

        [TestMethod]
        [TestCategory("Database Integration")]
        public void ContactRepositoryPersistWithNewContactExpectSaved()
        {
            var result = AddTestContact(DateTime.Now, ContactType.Telephone, DirectionType.Inbound, "Example");

            Assert.AreNotEqual(0, result.Id.Value);
        }

        [TestMethod]
        [TestCategory("Database Integration")]
        public void ContactRepositoryPersistWithExistingContactExpectSaved()
        {
            const string expectedSummary = "Test";
            var expectedDirection = DirectionType.Outbound;
            var expectedType = ContactType.InstantMessage;

            var testContact = AddTestContact(DateTime.Now, ContactType.Telephone, DirectionType.Inbound, "Example");

            testContact.Direction = expectedDirection;
            testContact.Type = expectedType;
            testContact.Summary = expectedSummary;

            var result = Target.Persist(testContact);

            Assert.AreEqual(testContact.Id, result.Id);
            Assert.AreEqual(expectedSummary, result.Summary);
            Assert.AreEqual(expectedDirection, result.Direction);
            Assert.AreEqual(expectedType, result.Type);
        }

        [TestMethod]
        [ExpectedException(typeof(InvalidEntityException))]
        public void ContactRepositoryPersistWithNullContactExpectException()
        {
            Target.Persist(null);
        }

        [TestMethod]
        [ExpectedException(typeof(InvalidEntityException))]
        public void ContactRepositoryPersistWithInvalidContactExpectException()
        {
            var testContact = Contact.NewContact();

            Target.Persist(testContact);
        }
    }
}
